package com.epoint.bio.socket;

import java.io.*;
import java.net.Socket;
import java.util.List;

public class ServerThread extends Thread{
	
	private List<Socket> sockets = null;
	
	public ServerThread(List<Socket> sockets) {
		this.sockets = sockets;
	}
 
	@Override
	public void run() {
		for(Socket socket : sockets){
            InputStream is=null;
            InputStreamReader isr=null;
            BufferedReader br=null;
            OutputStream os=null;
            PrintWriter pw=null;
            try {
                is = socket.getInputStream();
                isr = new InputStreamReader(is);
                br = new BufferedReader(isr);

                String info = br.readLine();
                socket.shutdownInput();

                os = socket.getOutputStream();
                pw = new PrintWriter(os);
                pw.write(info);

                pw.flush();
            } catch (Exception e) {
                // TODO: handle exception
            } finally{
                //关闭资源
                try {
                    if(pw!=null)
                        pw.close();
                    if(os!=null)
                        os.close();
                    if(br!=null)
                        br.close();
                    if(isr!=null)
                        isr.close();
                    if(is!=null)
                        is.close();
                    if(socket!=null)
                        socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
 
}